package com.ilop.sthome.ui.activity.device;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventGatewayInfo;
import com.ilop.sthome.data.event.EventRefreshDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.mvp.contract.device.RoomManageContract;
import com.ilop.sthome.mvp.present.device.RoomManagePresenter;
import com.ilop.sthome.ui.adapter.room.RoomManageAdapter;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityRoomManageBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：房间管理页面
 */
public class RoomManageActivity extends ContainLoginActivity<RoomManagePresenter, ActivityRoomManageBinding> implements RoomManageContract.IView {

    private boolean isBindRoom;
    private String mDeviceName;
    private int mDeviceId;
    private int mPosition = 0;
    private List<RoomBean> roomBeanList;
    private RoomManageAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_manage;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new RoomManagePresenter(mContext);
    }

    @Override
    protected void initialize() {
        super.initialize();
        isBindRoom = getIntent().getBooleanExtra(CommonId.KEY_BIND_ROOM, false);
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID, 0);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
    }

    @Override
    protected void initView() {
        super.initView();
        mPresent.refreshUI(mDeviceName, mDeviceId);
        mAdapter = new RoomManageAdapter(mContext, isBindRoom);
        mDBind.rvRoomList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.rvRoomList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        LiveDataBus.get().with(CommonId.ROOM_MANAGER, Integer.class).observe(this, integer -> {
            if (isBindRoom){
                mPosition = integer;
                mAdapter.setPosition(integer);
                mPresent.setSelectRoom(roomBeanList.get(mPosition), mPosition);
            }else {
                Intent intent = new Intent(mContext, RoomEditorActivity.class);
                intent.putExtra(CommonId.KEY_BIND_ROOM, integer);
                intent.putExtra(CommonId.KEY_MODIFY, true);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivRoomBack.setOnClickListener(v -> finish());
        mDBind.ivAddRoom.setOnClickListener(v -> skipAnotherActivity(RoomEditorActivity.class));
        mDBind.bindRoom.setOnClickListener(view -> {
            showProgressDialog();
            mPresent.onSaveChange();
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_DEVICE, EventRefreshDevice.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: subDevice " +  event.getDevice_status());
                if (event.getDeviceName().equals(mDeviceName) && event.getDevice_id() == mDeviceId){
                    bindSuccess();
                }
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_GATEWAY_INFO, EventGatewayInfo.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: gateway " +  event.getData_str2());
                if (event.getDeviceName().equals(mDeviceName)){
                    String roomId = event.getData_str2().substring(0, 2);
                    mPresent.modifySuccess(roomId);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.getRoomListByAccount();
        if (isBindRoom){
            DeviceInfoBean device = DeviceDaoUtil.getInstance().findByDeviceId(mDeviceName, mDeviceId);
            if ("00".equals(device.getRoom_id())){
                roomBeanList = RoomDaoUtil.getInstance().findRoomNotEmpty();
            }else {
                roomBeanList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
            }
            mPresent.getCurrentRoom(roomBeanList);
        }else {
            roomBeanList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
        }
        mAdapter.setList(roomBeanList);
    }

    @Override
    public void showIsModifyRoom() {
        mDBind.tvRoomTitle.setText(getString(R.string.modify_room));
        mDBind.tvRoomMsg.setText(getString(R.string.add_equipment_to_rooms));
        mDBind.bindRoom.setVisibility(View.VISIBLE);
    }

    @Override
    public void showCurrentRoom(int position) {
        mPosition = position;
        mAdapter.setPosition(position);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void bindSuccess() {
        dismissProgressDialog();
        showToast(getString(R.string.success));
        finish();
    }
}
